package com.woniu.service.impl;

import com.woniu.entity.CustomerInfos;
import com.woniu.entity.Finances;
import com.woniu.entity.Sales;
import com.woniu.mapper.FinancesMapper;
import com.woniu.service.CustomerInfosService;
import com.woniu.service.FinancesService;
import com.woniu.service.SalesService;
import com.woniu.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-04-01
 */
@Service
public class FinancesServiceImpl implements FinancesService {

    @Autowired
    private FinancesMapper financesMapper;

    @Autowired
    private SalesService salesService;

    @Autowired
    private CustomerInfosService customerInfosService;


    @Override
    public Finances findById(Integer financeId) {
        Finances finances = financesMapper.selectById(financeId);
        return finances;
    }

    /**
     * 增加一条财务记录
     * @return boolean
     */
    @Override
    public boolean save(Finances finances){

        //获取销售编号
        Integer salesId = finances.getSalesId();
        String s = String.valueOf(salesId);
        //根据销售编号查询销售表上的客户编号
        ResultVO resultVO = salesService.findById(Integer.valueOf(s));
        Map  resultVOData = (Map) resultVO.getData();
        Integer customerId = (Integer) resultVOData.get("customerId");
        System.out.println(customerId+"-------------------------");
        //Integer id = Integer.valueOf(customerId);
        //根据客户编号查询查询客户表上的用户编号
        CustomerInfos customerInfos = new CustomerInfos();
        customerInfos.setCustomerId(customerId);

        ResultVO resultVO1 = customerInfosService.GetCustomerInfoById(customerInfos);
        Map stringMap = (Map) resultVO1.getData();
        Integer userId1 = (Integer) stringMap.get("userId");

        Integer userId = Integer.valueOf(userId1);
        //存入表中
        finances.setUserId(userId);
        finances.setAddDate(new Date());
        int insert = financesMapper.insert(finances);
        if(insert < 1){
            return false;
        }
        return true;
    }
    /**
     * 删除功能，可批量删除
     * @param financeIds
     * @return boolean
     */
    @Override
    public boolean delete(List<Integer> financeIds){
        Finances finances = financesMapper.selectById(financeIds.get(0));
        Integer salesId = finances.getSalesId();
        salesService.deletes(salesId);
        int i = financesMapper.deleteBatchIds(financeIds);
        if(i < 1){
            return false;
        }
        return true;
    }
    @Override
    public boolean updata(Finances finances){
        int i = financesMapper.updateById(finances);
        if(i < 1){
            return false;
        }
        return true;
    }
    /**
     * 查询财务表的所有信息
     * @return
     */
    @Override
    public List<Map<String, String>> findAll(String type){
        List<Map<String, String>> finances = financesMapper.findFinances(type);
        return finances;
    }
}
